//
//  SwapFaceViewController.m
//  cam
//
//  Created by Le Hung on 12/21/13.
//  Copyright (c) 2013 DanielLe. All rights reserved.
//

#import "SwapFaceViewController.h"
#import "DataSingleton.h"
#import <QuartzCore/QuartzCore.h>
#import "DetailSwapFaceViewController.h"
#import "AmobiSDK.h"

@interface SwapFaceViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)NSArray *arrayItem;
@end

@implementation SwapFaceViewController
@synthesize arrayItem;

-(id)init{
    self = [super init];
    if (IS_IOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = YES;
    }
    
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.view addSubview:[AmobiSDK ShowAmobiSDK:CGPointMake(160, 50) :1]];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    UIImage *imgBg = [UIImage imageNamed:@"Puppet_City.jpg"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgBg.size.width, imgBg.size.height)];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.image = imgBg;
    [mScrollView addSubview:imgView];
    mScrollView.delegate = self;
    DataSingleton *data = [DataSingleton shareManager];
    arrayItem = [data arrayItem:0];
    for (int i = 0; i < arrayItem.count; i++) {
        CGRect frame;
        frame.origin.x = (mScrollView.frame.size.width * i ) + 60;
        frame.origin.y = 80;
		frame.size.height = self.view.bounds.size.height / 2;
        frame.size.width = self.view.bounds.size.width / 2 + 40;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.cornerRadius = 5.0;
        [btn.layer setMasksToBounds:YES];
        btn.layer.borderColor=[UIColor greenColor].CGColor;
        btn.layer.borderWidth=2.0f;
        [btn setFrame:frame];
        [btn setImage:[UIImage imageNamed:[arrayItem objectAtIndex:i]] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(touchIndex:) forControlEvents:UIControlEventTouchUpInside];
        [mScrollView addSubview:btn];
        
    }
    mScrollView.contentSize = CGSizeMake(mScrollView.frame.size.width * arrayItem.count , mScrollView.frame.size.height / 2);
    mPageControl.currentPage = 0;
    mPageControl.numberOfPages = arrayItem.count;

}

- (void)touchIndex:(UIButton*)sender
{
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"GroupSwapFaceViewController"];
    [vc setModalPresentationStyle:UIModalPresentationCustom];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = mScrollView.frame.size.width;
    int page = floor((mScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    mPageControl.currentPage = page;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end
